package switchviz.recording;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

import switchviz.core.ResourceManager;
import switchviz.util.FileOperations;

public class RecordPersistence 
{
	public final static String RECORDINFO = "_recordinfo";
	private final static String ROOT = "record";
	private final static String RECSTART = "start";
	private final static String RECEND = "end";
	private final static String TIMEINTERVALL = "timeintervall";
	private final static String SWITCHNAME = "switchname";
	private final static String SWITCHADDRESS = "switchaddress";
	private final static String SWITCHPORTCOUNT = "switchportcount";
	private final static String SWITCHPORT = "switchport";
	private final static String RECORDDATE = "recorddate";
	private static RecordPersistence _instance = null;
	
	private RecordPersistence()
	{

	}
	
	public static RecordPersistence getInstance()
	{
		if(_instance == null)
			_instance = new RecordPersistence();
		return _instance;
	}
	
	public void writeRecordInfo(RecordDataModel recdm, File path) throws IOException
	{
		Element root = new Element(ROOT);
		Element recstart = new Element(RECSTART);
		Element recend = new Element(RECEND);
		Element timeintervall = new Element(TIMEINTERVALL);
		Element switchname = new Element(SWITCHNAME);
		Element switchaddress = new Element(SWITCHADDRESS);
		Element recorddate = new Element(RECORDDATE);
		Element switchportcount = new Element(SWITCHPORTCOUNT);
		
		recstart.addContent(String.valueOf(recdm.getRecStart()));
		recend.addContent(String.valueOf(recdm.getRecEnd()));
		timeintervall.addContent(String.valueOf(recdm.getTimeintervall()));
		switchname.addContent(recdm.getSwitchName());
		switchaddress.addContent(recdm.getSwitchAddress());
		recorddate.addContent(recdm.getDate());
		
		for(String port : recdm.getSwitchPortCount())
		{
			Element switchport = new Element(SWITCHPORT);
			switchport.addContent(port);
			switchportcount.addContent(switchport);
		}
		
		root.addContent(recorddate);
		root.addContent(switchname);
		root.addContent(switchaddress);
		root.addContent(recstart);
		root.addContent(recend);
		root.addContent(timeintervall);
		root.addContent(switchportcount);
		
		Document doc = new Document(root);
		FileOperations.saveXML(new File(path, RECORDINFO), doc);
	}
	
	@SuppressWarnings("unchecked")
	public RecordDataModel readRecordInfo(File file) throws JDOMException, IOException
	{
		RecordDataModel rdm = new RecordDataModel();
		SAXBuilder parser = new SAXBuilder();
	    Document doc = parser.build(file);
	    Element root = doc.getRootElement();
	    List<String> switchport = new ArrayList<String>();
	    
	    if(root.getChild(RECSTART) != null)
	    	rdm.setRecStart(Long.valueOf(root.getChildText(RECSTART)));
	    if(root.getChild(RECEND) != null)
	    	rdm.setRecEnd(Long.valueOf(root.getChildText(RECEND)));
	    if(root.getChild(TIMEINTERVALL) != null)
	    	rdm.setTimeintervall(Integer.valueOf(root.getChildText(TIMEINTERVALL)));
	    if(root.getChild(SWITCHNAME) != null)
	    	rdm.setSwitchName(root.getChildText(SWITCHNAME));
	    if(root.getChild(SWITCHADDRESS) != null)
	    	rdm.setSwitchAddress((root.getChildText(SWITCHADDRESS)));
	    if(root.getChild(RECORDDATE) != null)
	    	rdm.setDate((root.getChildText(RECORDDATE)));
	    if(root.getChild(SWITCHPORTCOUNT) != null)
	    {
	    	Iterator<Element> it = root.getChild(SWITCHPORTCOUNT).getChildren(SWITCHPORT).iterator();
	    	while(it.hasNext())
	    		switchport.add(it.next().getText());
	    	
	    	rdm.setSwitchPortCount(switchport);
	    }
	    	
	    
	    return rdm;
	}
	
	public List<RecordDataModel> getRecordList() throws JDOMException, IOException
	{
		ArrayList<RecordDataModel> reclist = new ArrayList<RecordDataModel>();
		File folder = ResourceManager.getInstance().getRecordFolder();
		File subfolder[] = folder.listFiles();
		for(File recfolder : subfolder)
		{
			if(recfolder.isFile()) continue;
			File temp = new File(recfolder, RECORDINFO + ".xml");
			if(temp.exists())
			{
				RecordDataModel rdm = readRecordInfo(temp);
				rdm.setFolder(recfolder);
				reclist.add(rdm);
			}
		}
		return reclist;
	}
	
	public List<File> getFileList(File folder)
	{
		ArrayList<File> filelist = new ArrayList<File>();
		for (File file : folder.listFiles())
		{
			if(!file.getName().equals(RECORDINFO + ".xml") && !file.isHidden())
				filelist.add(file);
		}
		return filelist;
	}
}
